package com.frxuexi.mynews.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.frxuexi.mynews.entity.NewsEntity
import com.frxuexi.mynews.intent.NewsIntent
import com.frxuexi.mynews.intent.NewsTypeIntent
import com.frxuexi.mynews.repository.NewsTypeRepository
import com.frxuexi.mynews.state.NewsState
import com.frxuexi.mynews.state.NewsTypeState
import com.google.gson.Gson
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.RequestBody

class NewsViewModel:ViewModel() {
    val resp by lazy { NewsTypeRepository() }

    val channel= Channel<NewsIntent>(Channel.UNLIMITED)

    private var _state= MutableStateFlow<NewsState>(NewsState.Loading)
    val newsState: StateFlow<NewsState>
        get() = _state

    init {
        handlerIntent()
    }

    private fun handlerIntent() {
        viewModelScope.launch {
            channel.consumeAsFlow().collect{
                when(it){
                    is NewsIntent.getNews->
                        getNews(it.page,it.type)
                    is NewsIntent.getNewsDetail->
                        getNewsDetail(it.id)
                    is NewsIntent.newsCollect->
                        newsCollect(it.nid)
                    is NewsIntent.myCollect->
                        myCollect()
                    is NewsIntent.newsComment->
                        newsComment(it.map)
                    is NewsIntent.getCommentById->
                        getCommentById(it.nid)
                    else -> {}
                }
            }
        }

    }

    private fun getCommentById(nid: Int) {
        viewModelScope.launch {
            resp.getCommentsById(nid).collect{
                if(it.code==0){
                    _state.value= NewsState.ongetCommentSucess(it.data)
                }else{
                    _state.value= NewsState.onError(it.msg)
                }
            }
        }
    }

    private fun newsComment(map: Map<String, Any>) {
        val json = Gson().toJson(map)
        val body = RequestBody.create(MediaType.parse("application/json"), json)
        viewModelScope.launch {
            resp.newsComment(body).collect{
                if(it.code==0){
                    _state.value= NewsState.onNewsCommentSucess(it.msg)
                }else{
                    _state.value= NewsState.onError(it.msg)
                }
            }
        }
    }

    private fun myCollect() {
        viewModelScope.launch {
            resp.myCollect().collect{
                if(it.code==0){
                    _state.value= NewsState.onMyCollectSucess(it.data)
                }else{
                    _state.value= NewsState.onError(it.msg)
                }
            }
        }
    }

    private fun newsCollect(nid: Int) {
        viewModelScope.launch {
            resp.newsCollect(nid).collect{
                if(it.code==0){
                    _state.value= NewsState.onNewsCollectSucess(it.data)
                }else{
                    _state.value= NewsState.onError(it.msg)
                }
            }
        }
    }

    fun getNewsDetail(id:Int){
        viewModelScope.launch {
            resp.getNewsDetail(id).collect{
                if(it.code==0){
                    _state.value= NewsState.onDetailSucess(it.data)
                }else{
                    _state.value= NewsState.onError(it.msg)
                }
            }
        }
    }

    fun getNews(page:Int,type:Int){
//        val json = Gson().toJson(map)
//        val body = RequestBody.create(MediaType.parse("application/json"), json)
        viewModelScope.launch {
            resp.getNews(page, type).collect{
                if(it.code==0){
                    val list = arrayListOf<NewsEntity>()
                    it.data.records.forEachIndexed { index, it ->
                        list.add(NewsEntity(it.content,it.createTime,it.flag,it.id,it.imgurl,it.looks,it.ntid,it.suid,it.title,index%2))
                    }

                    _state.value= NewsState.onSucess(list)
                }else{
                    _state.value= NewsState.onError(it.msg)
                }
            }
        }
    }

}